package com.servlet;

import com.dao.imp.AddUpdDelDaoImp;
import com.dao.imp.FindOrder;
import com.dao.imp.MerchantLogin;
import com.entity.Merchant;
import com.entity.Order;
import com.util.SendMail;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/12/3 17:10
 **/

@WebServlet(name = "PaySuccessfulServlet", value = "/PaySuccessfulServlet")
public class PaySuccessfulServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println(request.getParameter("out_trade_no"));
        String order_id = request.getParameter("out_trade_no");
        String sql = "UPDATE orders SET isPay = 1 WHERE order_id = "+order_id;
        System.out.println(sql);
        AddUpdDelDaoImp addUpdDelDaoImp = new AddUpdDelDaoImp();
        addUpdDelDaoImp.addUpdDelGoods(sql);


        String selectSql = "SELECT * FROM orders WHERE order_id = "+order_id;
        Order order = FindOrder.getOrderInfo(selectSql);
        String good  = order.getGood().toString();
        String[] array = good.split("merchant_id\":");
        String merchant_id = String.valueOf(array[1].substring(0,4));
        MerchantLogin merchantLogin = new MerchantLogin();
        Merchant merchant = merchantLogin.getUserInfo(merchant_id);

        System.out.println(merchant);
        System.out.println(merchant.getMerchant_email());
        try {
            SendMail.sendMail(merchant.getMerchant_email(),order_id+"已完成支付，请您及时登录"+merchant_id+"账号及时进行处理","新的订单已完成支付，请及时发货");
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.sendRedirect("user_center.html");
    }
}
